package com.gzbugu.polytechsasia.bean;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页模型
 * 
 * @author gauze
 * 
 */
public class PageModel {
	private Integer currentPage; // 当前页面
	private Integer totalPages; // 总页数
	private Integer pageSize; // 每页记录条数
	private Integer lineCount; // 记录总数

	public PageModel() {
		this.currentPage = 1;
		this.lineCount = 0;
		this.pageSize = 5;
		this.totalPages = 1;
	}

	public PageModel(int currentPage, int pageSize, int lineCount) {
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		if (pageSize < 5)
			this.pageSize = 5;// pageSize至少为5（默认）
		this.lineCount = lineCount;
		this.totalPages = (lineCount + pageSize - 1) / pageSize;
		if (totalPages == 0)
			this.totalPages = 1;
		if (currentPage > totalPages)
			this.currentPage = totalPages;
		if (currentPage < 1)
			this.currentPage = 1;
	}

	public Integer hasNext() {
		if (currentPage < totalPages)
			return currentPage + 1;
		return null;
	}

	public Integer hasPrevious() {
		if (currentPage > 1)
			return currentPage - 1;
		return null;
	}

	@SuppressWarnings("unchecked")
	public List getPageList() {
		List pageList = new ArrayList();
		for (Integer i = 1; i <= totalPages; i++) {
			pageList.add(i);
		}
		return pageList;
	}

	/**
	 * setter后更新page信息
	 */
	public void update() {
		this.totalPages = (lineCount + pageSize - 1) / pageSize;
		if (totalPages == 0)
			this.totalPages = 1;
		if (currentPage > totalPages)
			this.currentPage = totalPages;
	}

	public Integer getCurrentPage() {
		update();
		return currentPage;
	}

	public Integer getTotalPages() {
		update();
		return totalPages;
	}

	public Integer getLineCount() {
		update();
		return lineCount;
	}

	public Integer getPageSize() {
		update();
		return pageSize;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
		if (pageSize < 5)
			this.pageSize = 5;// pageSize至少为5（默认）
	}

	public void setLineCount(Integer lineCount) {
		this.lineCount = lineCount;
	}

}
